<?php
namespace App\Model\Movie;
use PhalApi\Model\DataModel;

class MovieList extends DataModel {

    protected function getTableName($id) {
        return 'list';
    }

    public function getPerList($limit,$page) {
        $res = $this->getORM()->select('*')->where('show_date >=?',2000)->where('show_date <=?',2020)->order('show_date DESC')->limit($page*$limit,$limit)->fetchAll();
        $total = $this->getORM()->select('count(id) as total')->where('show_date >=?',2000)->where('show_date <=?',2020)->fetchOne();
        foreach ($res as $key=>$value) {
            $res[$key] = array_merge($value, array('show' => false));
        }
        return ['list'=>$res,'total'=>$total['total']];
    }
    public function getHome($ids){
        $res = $this->getORM()->select('id,title,introduce,img,add_date,source_id')->where('cate_id',$ids)->order('show_date DESC')->limit(51)->fetchAll();
        $med = [];
        $tuijian =[];
        $today = [];
        $yestoday = [];
        $new = [];
        $week = [];
        foreach ($res as $k=>$v){
            $res[$k]['date']=substr($v['add_date'],3);
            $res[$k]['status']='全集';
            $res[$k]['src']=$v['img'];
            unset($res[$k]['add_date']);
            unset($res[$k]['img']);
            if($k == 0){
                $res[$k]['introduce']=substr($v['introduce'],0,-57);
                $med=$res[$k];
            }elseif($k>0&&$k<11) {
                $tuijian[] = $res[$k];
            }elseif($k<21){
                $res[$k]['date']=date("m-d",time());
                unset($res[$k]['introduce']);
                unset($res[$k]['source_id']);
                unset($res[$k]['status']);
                unset($res[$k]['src']);
                $today[] = $res[$k];
            }elseif($k<31){
                $res[$k]['date']=date("m-d",strtotime("-1 day"));
                unset($res[$k]['introduce']);
                unset($res[$k]['source_id']);
                unset($res[$k]['status']);
                unset($res[$k]['src']);
                $yestoday[] = $res[$k];
            }elseif($k<41){
                $res[$k]['date']=date("m-d",time());
                unset($res[$k]['introduce']);
                unset($res[$k]['source_id']);
                unset($res[$k]['status']);
                unset($res[$k]['src']);
                $new[] = $res[$k];
            }elseif($k<51){
                $day = rand(1,7);
                $res[$k]['date']=date("m-d",strtotime("-$day day"));
                unset($res[$k]['introduce']);
                unset($res[$k]['source_id']);
                unset($res[$k]['status']);
                unset($res[$k]['src']);
                $week[] = $res[$k];
           }
        }
        return ['med'=>$med,'tuijian'=>$tuijian,'today'=>$today,'yestoday'=>$yestoday,'new'=>$new,'week'=>$week];
    }
    public function getMovieByPid($ids,$limit,$page=0){
        $res = $this->getORM()->select('*')->where('cate_id',$ids)->where('show_date >=?',2000)->where('show_date <=?',2020)->order('show_date DESC')->limit($page*$limit,$limit)->fetchAll();
        $total = $this->getORM()->select('count(*) as total')->where('cate_id',$ids)->where('show_date >=?',2000)->where('show_date <=?',2020)->fetchOne();
        return ['list'=>$res,'total'=>$total['total']];
    }
    public function getMovieByYear($ids,$year,$limit,$page=0){
        $res = $this->getORM()->select('*')->where('cate_id',$ids)->where('show_date',$year)->order('show_date DESC')->limit($page*$limit,$limit)->fetchAll();
        $total = $this->getORM()->select('count(*) as total')->where('cate_id',$ids)->where('show_date',$year)->fetchOne();
        return ['list'=>$res,'total'=>$total['total']];
    }
    public function getMovieYearsByPid($ids){
        $res = $this->getORM()->select('show_date')->where('cate_id',$ids)->where('show_date >=?',2000)->where('show_date <=?',2020)->group('show_date')->fetchAll();
        return $res;
    }
    public function getMovieInfo($id) {
        $res = $this->getORM()->select('*')->where('source_id',$id)->fetchOne();
        return $res;
    }
    public function searchMovieList($title,$limit,$page=0) {
        $res = $this->getORM()->select('*')->where(' title LIKE ?',"%$title%")->where('show_date >=?',2000)->where('show_date <=?',2020)->order('show_date DESC')->limit($page*$limit,$limit)->fetchAll();
        $total = $this->getORM()->select('count(*) as total')->where(' title LIKE ?',"%$title%")->where('show_date >=?',2000)->where('show_date <=?',2020)->fetchOne();
        return ['list'=>$res,'total'=>$total['total']];
    }
    public function domainMovieList($domain,$limit,$page=0) {
        $res = $this->getORM()->select('*')->where('domain',$domain)->where('show_date >=?',2000)->where('show_date <=?',2020)->order('show_date DESC')->limit($page*$limit,$limit)->fetchAll();
        $total = $this->getORM()->select('count(*) as total')->where('domain',$domain)->where('show_date >=?',2000)->where('show_date <=?',2020)->fetchOne();
        return ['list'=>$res,'total'=>$total['total']];
    }

    public function getLikeList() {
        $res = $this->queryAll("SELECT * FROM `list` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `list` WHERE `show_date` BETWEEN 2000 AND 2020)-(SELECT MIN(id) FROM `list` WHERE `show_date` BETWEEN 2000 AND 2020))+(SELECT MIN(id) FROM `list` WHERE `show_date` BETWEEN 2000 AND 2020)) AS id) AS t2
WHERE t1.id >= t2.id AND `show_date` BETWEEN 2000 AND 2020
ORDER BY t1.id LIMIT 14");
        return $res;
    }

    public function searchList($title,$limit,$page=0){
        $res = $this->getORM()->select('*')->where(' title LIKE ?',"%$title%")->where('show_date >=?',2000)->where('show_date <=?',2020)->order('show_date DESC')->limit($page*$limit,$limit)->fetchAll();
        $total = $this->getORM()->select('count(*) as total')->where(' title LIKE ?',"%$title%")->where('show_date >=?',2000)->where('show_date <=?',2020)->fetchOne();
        foreach ($res as $key=>$value){
            $res[$key]=array_merge($value,array('show'=>false));
        }
        return ['list'=>$res,'total'=>$total['total']];
    }
    public function setDomains($domains){
        $res = $this->getORM()->where('domain IS NULL')->min('id');
        $first = $this->getORM()->alias('a')->where('a.id',$res-1)->leftJoin('domain','b','a.domain = b.name')->select('a.id as aid,b.id as bid')->fetchone();
        if($first && $first['bid']<count($domains)) {
            $last = [];
            foreach ($domains as $key => $value) {
                if ($first['bid'] <= $key) {
                   $last[]= $value;
                }
            }
            $list = $this->getORM()->where('domain IS NULL')->select('id')->limit(count($last))->fetchAll();
            foreach ($list as $k=>$v){
                self::updateAll(['id'=>$v['id']],['domain'=>$last[$k%count($last)]['name']]);
            }
        }
        $list = $this->getORM()->where('domain IS NULL')->select('id')->fetchAll();
        foreach ($list as $k=>$v){
            self::updateAll(['id'=>$v['id']],['domain'=>$domains[$k%count($domains)]['name']]);
        }
        return 1;
    }
    public function setDomain($domain,$newdata,$domains){
        $res = self::updateAll(['domain'=>$domain],['domain'=>$newdata]);
        foreach ($res as $k=>$v){
            self::update($v,['domain'=>$domains[$k%count($domains)]['domain']]);
        }
        return 1;
    }
}